var prefix1 = ctx + "synchronize/monitor";
var data_info = [];
var map = new BMap.Map("allmap",{enableMapClick: false,minZoom:6,maxZoom:12 });
map.enableScrollWheelZoom(true);
map.centerAndZoom(new BMap.Point(108.217330299,31.0426112029), 7);
map.setMapStyleV2({     
	  styleId: '6c49226af0595bc298a31b5a830a8e5c'
	});
$(function () {
	
	loadMap(); //初始化地图数据
	
});



function getBoundary(map,city){
    var bdary = new BMap.Boundary();
    bdary.get(city, function(rs){//获取行政区域
//        map.clearOverlays();//清除地图覆盖物
        //思路：利用行政区划点的集合与外围自定义东南西北形成一个环形遮罩层
        //1.获取选中行政区划边框点的集合rs.boundaries[0]
    	var list =rs.boundaries;
    	var num="";
    	var max  = list[0];
    	for (var i = 1; i < list.length; i++) {
    		if (list[i].length > max.length){ 
    			max = list[i]; 
    		}
		}
        	var strs = new Array();
        	strs = max.split(";");
        	var ENWS = "";
        	for (var i=0;i<strs.length;i++) {
        		ENWS += strs[i] + ";"
        	}
        	//2.自定义外围边框点的集合
        	var E_JW = "170.672126, 39.623555;";        //东
        	var EN_JW = "170.672126, 81.291804;";       //东北角
        	var N_JW = "105.913641, 81.291804;";        //北
        	var NW_JW = "-169.604276,  81.291804;";     //西北角
        	var W_JW = "-169.604276, 38.244136;";       //西
        	var WS_JW = "-169.604276, -68.045308;";     //西南角
        	var S_JW = "114.15563, -68.045308;";        //南
        	var SE_JW = "170.672126, -68.045308 ;";     //东南角
        	//3.添加环形遮罩层
        	var ply1 = new BMap.Polygon(ENWS + E_JW + SE_JW + S_JW + WS_JW + W_JW + NW_JW + N_JW + EN_JW + E_JW, {
        		strokeColor:"none",
        		strokeOpacity:0,
        		fillColor:"#0f1f40",
        		fillOpacity:"1"
        			}); //建立多边形覆盖物
        	map.addOverlay(ply1);//遮罩物是半透明的，如果需要纯色可以多添加几层
        	//4. 给目标行政区划添加边框，其实就是给目标行政区划添加一个没有填充物的遮罩层
        	var ply = new BMap.Polygon(max, {strokeWeight:1,strokeColor:"#064fa0",fillColor: "#00ffff", //设置多边形填充颜色
                fillOpacity:0.04});
        	map.addOverlay(ply);
        	map.setViewport(ply.getPath());//调整视野
        	
        
    });
}
function getBoundary1(map,city){
	var bdary = new BMap.Boundary();
	bdary.get(city, function(rs){//获取行政区域
		
		var list = rs.boundaries;
		var num="";
		var max  = list[0];
		for (var i = 1; i < list.length; i++) {
			if (list[i].length > max.length){ 
				max = list[i]; 
			}
		}
		//4. 给目标行政区划添加边框，其实就是给目标行政区划添加一个没有填充物的遮罩层
		var ply = new BMap.Polygon(
				max, 
					{
					strokeWeight:1,
					strokeColor:"#064fa0",
					fillColor: "", //设置多边形填充颜色
						}
				);
		
		var cp= (ply.getBounds()).getCenter();
		var point = new BMap.Point(cp.lng,cp.lat);
		var label = new BMap.Label(city, {offset:new BMap.Size(-20,-10), position:point});  // 创建文本标注对象
			label.setStyle({
				 color : "#00f6df",
				 backgroundColor :"0",
				 fontSize : "14px",
				 border :"0"
			 });
			
		map.addOverlay(label);    //加标题
		
		map.addOverlay(ply);
	});

	
}
function getBoundary2(map,city){
	var bdary = new BMap.Boundary();
	bdary.get(city, function(rs){//获取行政区域
		
		var list = rs.boundaries;
		var num="";
		var max  = list[0];
		for (var i = 1; i < list.length; i++) {
			if (list[i].length > max.length){ 
				max = list[i]; 
			}
		}
		//4. 给目标行政区划添加边框，其实就是给目标行政区划添加一个没有填充物的遮罩层
		var ply = new BMap.Polygon(
				max, 
				{
					strokeWeight:1,
					strokeColor:"#064fa0",
					fillColor: "", //设置多边形填充颜色
				}
		);
		
		var cp= (ply.getBounds()).getCenter();
		var point = new BMap.Point(cp.lng,cp.lat);
		var label = new BMap.Label(city, {offset:new BMap.Size(-20,-10), position:point});  // 创建文本标注对象
		label.setStyle({
			color : "#f6ee00",
			backgroundColor :"0",
			fontSize : "24px",
			border :"0"
		});
		
		map.addOverlay(label);    //加标题
		
		map.addOverlay(ply);
	});
	
	
}
	



//弹出框高宽
var lwhith = 780;
var lheight = 520;

function loadMap() {
	map.clearOverlays(); 
	data_info.splice = 0;
	var obj = {};

    obj.dprtcode = $("#dprtcode").val();
	
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix1 + "/getMapData",
        async: false,
        data: obj,
        error: function (request) {
            $.modal.alertError("系统错误");
        },
        success: function (data) {
            data_info = [];
            if (data.length > 0) { 
            
        		 $.each(data, function (index, row) {
        			 var json1 = [];
        				 json1.push(row.longitude);
        				 json1.push(row.latitude);
        				 json1.push(row.boxName);
        				 
        				 var flag1="false";
        				 var flag="false";
        				 
        				 if(row.deviceMainList.length==0){
	        				 flag1="true";
	        			 }else{
	        				 $.each(row.deviceMainList, function (index1, row1) {
	        					 if (new Date(row1.updatetime).toDateString() === new Date().toDateString()) {
	        						 flag="true";
	        					 }
	        				 })
	        			 }
        				 
        				 var kfType="";
        				 if(row.storehouseId == 3){  //库房类型为试验
        					  kfType=2;
        				 }else{
        					 kfType=1;
        				 }
        				 
        				 
        				 json1.push(row.id);
        				 json1.push(flag);
        				 json1.push(flag1);
        				 json1.push(kfType);
        				 data_info.push(json1);
                 })
            }
            
  
            
            initMap();
            
            
           
            
        }
    });
}


function initMap(){
	for(var i=0;i<data_info.length;i++){
		if(data_info[i][6] == 1){
			if(data_info[i][4]=="true"){
				var icon = new BMap.Icon('/img/pin_green_solid_23.148936170213px_1129801_easyicon.net.png', new BMap.Size(23, 32), {   //改变标注样式绿色
					anchor: new BMap.Size(10, 0)      //这个是信息窗口位置（可以改改看看效果）
				});
			}else{
				var icon = new BMap.Icon('/img/pin_yellow_solid_23.148936170213px_1129897_easyicon.net.png', new BMap.Size(23, 32), {   //改变标注样式黄色
					anchor: new BMap.Size(10, 0)      //这个是信息窗口位置（可以改改看看效果）
				});
			}
			
			if(data_info[i][5]=="true"){
				var icon = new BMap.Icon('/img/pin_red_solid_23.148936170213px_1129865_easyicon.net.png', new BMap.Size(23, 32), {   //改变标注样式红色
					anchor: new BMap.Size(10, 0)      //这个是信息窗口位置（可以改改看看效果）
				});
			}
		}else{
			
			var icon = new BMap.Icon('/img/pin_blue_solid_23.148936170213px_1129769_easyicon.net.png', new BMap.Size(23, 32), {   //试验
				anchor: new BMap.Size(10, 0)      //这个是信息窗口位置（可以改改看看效果）
			});
		}
		var marker = new BMap.Marker(new BMap.Point(data_info[i][0],data_info[i][1]), { //加载图标和坐标
			icon: icon
		});
		var label = new BMap.Label(data_info[i][2],{offset:new BMap.Size(20,-15)}); //加载库房标题
		label.setStyle({
            display:"none",
        });
		
		 marker.setLabel(label);
		
		 marker.addEventListener("mouseover", function(e) {
             var label = this.getLabel()
             label.setStyle({ display: "block" });
         });
         marker.addEventListener("mouseout", function(e) {
             var label = this.getLabel()
             label.setStyle({ display: "none" });
              
         });
         
		var content = data_info[i][3]; //加载数据
		map.getPanes().markerPane.style.zIndex = 500;
		map.addOverlay(marker);               // 将标注添加到地图中
		addClickHandler(content,marker);
	}
	
	
	
  	getBoundary(map,$('#deptName').val());
  	 getBoundary2(map,$('#deptName').val()); //划出市区线
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix1 + "/initCity",
		async : false,
		data :{extName:$('#deptName').val()},
		error : function(request) {
			$.modal.alertError("系统错误");
		},
		success : function(data) {
		
			 $.each(data, function (index, row) {
				 getBoundary1(map,row.extName); //划出市区线
			 });
		}
	});
	
}




window.lastInfoBox = null;//定义上一个窗体为lastInfoBox;
function addClickHandler(content,marker){
	
	marker.addEventListener("click",function(e){
	
		
		var obj = {};
		var load = layer.load(1);
	    obj.id = content;
	    $.ajax({
	        cache: true,
	        type: "POST",
	        url: prefix1 + "/getMapDataById",
	        async: false,
	        data: obj,
	        error: function (request) {
	            $.modal.alertError("系统错误");
	        },
	        success: function (data) {
	        	 layer.close(load);      //关闭加载层
	            if (data.length > 0) { 
	            
	            	var str=initData(data[0]);
	            	
	            	var p = e.target;
	        		var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
	        		
	        		var infoBox = new BMapLib.InfoBox(window.map,str,{
	        		        boxStyle:{
	        		            width: "481px",
	        		            height: "230px",
	        		            marginBottom: "5px",
	        		            background:"url(../img/pop.png) center no-repeat "
	        		        },
	        		        closeIconMargin: "10px 10px 10px 10px",
	        		        closeIconUrl: "../img/cancel.png",
	        		        enableAutoPan: false,
	        		        align: INFOBOX_AT_TOP,
	        		    });
	        			if(lastInfoBox){
	        			//判断上一个窗体是否存在，若存在则执行close
	        		        lastInfoBox.close();
	        		    }
	        		    lastInfoBox = infoBox;
	        		    infoBox.uid = "2";   //标识 
	        		    infoBox.enableAutoPan(); //自动偏移
	        		    infoBox.open(point);
	            }
	            
	        }
	    });
		
		
		
		
		

	});
	
}


function initData(row){
	
	 var str="";
	 
	 var ww="";//外温
	 var nw="";//内温
	 var ns="";//内湿
	 
	 if(row.deviceMainList.length==0){
		 flag1="true";
	 }else{
		 var num1=0;
		 $.each(row.deviceMainList, function (index1, row1) {
			 if (new Date(row1.updatetime).toDateString() === new Date().toDateString()) {
				 flag="true";
				 num1++;
			 }else{
				 num1++;
			 } 
			 
		 })
	 }
	 
	 /** 设备总数 开始*/
	 if(num1==undefined){
		 num1=0;
	 }
	 var mycode=blstr(num1).split("");  
	 
	
	 /** 设备总数 结束*/
	 
	 str+='<div style="text-align:center;color:#ffffff;margin-top: 10px;" >'+row.boxName+'</div>';
	 //标题
	 str+='<div  style="margin-top: 6px;width:100%;height:20px;">';
	 var kfType="";
	 if(row.storehouseId == 3){  //库房类型为试验
//		 str+='<div  style="margin-top: 34px;width:100%;height:20px;">';
//		 str+='<div style="text-align:center;color:#ef4d4de0;margin-top: 10px;" >暂无库房温湿度数据</div>';
		  kfType=2;
		  str+='<div  style="text-align:center;color:#ebe732;width:25%;height:20px; float: left;">';
		  str+='检测总数	</div>';
		  
		  str+='<div  style="text-align:center;color:#ebe732;width:25%;height:20px;float: left;">';
		  str+='合格数</div>';
		  
		  str+='<div  style="text-align:center;color:#ebe732;width:25%;height:20px;float: left;">';
		  str+='合格率</div>';
	 }else{
//		 str+='<div  style="margin-top: 6px;width:100%;height:20px;">';
		 kfType=1;
		 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:20px; float: left;">';
		 str+='设备总数	</div>';
		 
		 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:20px;float: left;">';
		 str+='库存总数</div>';
		 
		 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:20px;float: left;">';
		 str+='年度报废总数</div>';
		 
		 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:20px;float: left;">';
		 str+='未归还总数</div>';
	 }
	 
	 
	 str+='</div>';
	 if(row.storehouseId != 3){  //库房类型为试验
	 //设备总数
	 str+='<div  style="margin-top: 5px;width:100%;height:20px;">';
	 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:30px; float: left;">';
	 var color="";
			for (var i = 0; i < mycode.length; i++) {
				if(mycode[i] != 0){
					color="#f12727";
				}
				
				
				
				if(i==0){
					str+='<div  style="color:'+color+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left;line-height: 30px;margin-left: 10px;">'+mycode[i]+'</div>';
				}else{
					str+='<div  style="color:'+color+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left; line-height: 30px;">'+mycode[i]+'</div>';
				}
			}
	 str+='</div>';
	 
	 //库存总数
	 var toolsNums=blstr(row.toolsNums).split("");  
	 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:30px;float: left;">';
	 var color1="";
		for (var i = 0; i < toolsNums.length; i++) {
			if(toolsNums[i] != 0){
				color1="#f12727";
			}
			if(i==0){
				str+='<div  style="color:'+color1+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left;line-height: 30px;margin-left: 10px;">'+toolsNums[i]+'</div>';
			}else{
				str+='<div  style="color:'+color1+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left; line-height: 30px;">'+toolsNums[i]+'</div>';
			}
		}
	 str+='</div>';
	 
	 //报废总数
	 var scrapNums=blstr(row.scrapNums).split("");  
	 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:30px;float: left;">';
	 var color2="";
	 	for (var i = 0; i < scrapNums.length; i++) {
			if(scrapNums[i] != 0){
				color2="#f12727";
			}
			if(i==0){
				str+='<div  style="color:'+color2+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left;line-height: 30px;margin-left: 10px;">'+scrapNums[i]+'</div>';
			}else{
				str+='<div  style="color:'+color2+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left; line-height: 30px;">'+scrapNums[i]+'</div>';
			}
		}
	 str+='</div>';
	 
	 //未归还总数
	 var noReturnedNums=blstr(row.noReturnedNums).split("");  
	 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:30px;float: left;">';
	 var color3=0;
	 	for (var i = 0; i < noReturnedNums.length; i++) {
			if(noReturnedNums[i] != 0){
				color3="#f12727";
			}
			if(i==0){
				str+='<div  style="color:'+color3+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left;line-height: 30px;margin-left: 10px;">'+noReturnedNums[i]+'</div>';
			}else{
				str+='<div  style="color:'+color3+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left; line-height: 30px;">'+noReturnedNums[i]+'</div>';
			}
		}
	 	
	 	
	 str+='</div>';
	 str+='</div>';
	 
	 
	 }else{
		 
		 str+='<div  style="margin-top: 5px;width:100%;height:20px;">';
		 
		 //库存总数
		 
		 var toolsNums=blstr(row.toolsNums).split("");  
		 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:30px;float: left;">';
		 var color1="";
			for (var i = 0; i < toolsNums.length; i++) {
				if(toolsNums[i] != 0){
					color1="#f12727";
				}
				if(i==0){
					str+='<div  style="color:'+color1+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left;line-height: 30px;margin-left: 10px;">'+toolsNums[i]+'</div>';
				}else{
					str+='<div  style="color:'+color1+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left; line-height: 30px;">'+toolsNums[i]+'</div>';
				}
			}
		 str+='</div>';
		 
		 
		 var scrapNums=blstr(row.scrapNums).split("");  
		 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:30px;float: left;">';
		 var color2="";
		 	for (var i = 0; i < scrapNums.length; i++) {
				if(scrapNums[i] != 0){
					color2="#f12727";
				}
				if(i==0){
					str+='<div  style="color:'+color2+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left;line-height: 30px;margin-left: 10px;">'+scrapNums[i]+'</div>';
				}else{
					str+='<div  style="color:'+color2+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left; line-height: 30px;">'+scrapNums[i]+'</div>';
				}
			}
		 str+='</div>';
		
		 
		 //报废总数
		 var noReturnedNums=blstr(row.noReturnedNums).split("");  
		 str+='<div  style="text-align:center;color:#32EBCA;width:25%;height:30px;float: left;">';
		 var color2="";
		 	for (var i = 0; i < noReturnedNums.length; i++) {
				if(noReturnedNums[i] != 0){
					color2="#f12727";
				}
				if(i==0){
					str+='<div  style="color:'+color2+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left;line-height: 30px;margin-left: 10px;">'+noReturnedNums[i]+'</div>';
				}else{
					str+='<div  style="color:'+color2+';background:url(../img/num-bg.c775211.png) center no-repeat;width:26px;height:30px;float: left; line-height: 30px;">'+noReturnedNums[i]+'</div>';
				}
			}
		 str+='</div>';

		 
		 
		 
		 str+='</div>';
		 str+='</div>';
	 }
	 
	 
	 
	 
	 str+='<p style="border-bottom:solid 1px rgba(53, 131, 158, 0.8);margin: 18px 16px 2px"></p>';
	 var ww="";
	 var nw="";
	 var ns="";
	 if(row.type == "EM"){
		 ww=(formatUndefine(row.avgww)==""?"":formatUndefine(row.avgww)+" ℃");
		 nw=(formatUndefine(row.avgnw)==""?"":formatUndefine(row.avgnw)+" ℃");
		 ns=(formatUndefine(row.avgns)==""?"":formatUndefine(row.avgns)+" %");
	 }
	 
	 if(row.storehouseId == 1){  //库房类型为试验
		 //内容
		 str+='<div  style="height:30px;width:100%;">';
		 str+='<div  style="text-align:left;color:#ffffff;width: 30%;margin-left: 23px;height:20px; float: left;    font-size: 18px;">';
		 str+='外温：<span style="color:#25ff00">'+ww+'<span/></div>';
		 str+='<div  style="text-align:left;color:#ffffff;width:30%;height:20px;float: left;    font-size: 18px;">';
		 str+=' 内温：<span style="color:#25ff00">'+nw+'<span/></div>';
		 str+='<div  style="text-align:left;color:#ffffff;width:30%;height:20px;float: left;    font-size: 18px;">';
		 str+=' 内湿：<span style="color:#25ff00">'+ns+'<span/></div>';
		 str+='</div>';
		 //站点名称
		 str+='<div  style="margin-top: 2px;height:30px;width:100%;">';
		 str+='<div  style="text-align:center;color:#ffffff;height:20px; float: left;    font-size: 18px;    margin-left: 23px;"> 站点名称： '+row.deptName+'</div>';
		 
		 str+='</div>';
		 //地址
		 str+='<div  style="margin-top: px;height:30px;width:100%;">';
		 str+='<div  style="text-align:center;color:#ffffff;height:20px; float: left;    font-size: 18px;    margin-left: 23px;"> 地址： <span style="font-size: 15px;">'+row.remark+'<span/></div>';
	
	 }else if(row.storehouseId == 3){
		 
		 str+='<div  style="margin-top: 0px;height:30px;width:100%;">';
		 str+='<div  style="text-align:center;color:#ffffff;height:20px; float: left;    font-size: 18px;    margin-left: 23px;"> 最近检测时间： '+(row.testDate == null ? '' : (row.testDate).substring(0,10))+'</div>';
		 
		 str+='</div>';

		 //站点名称
		 str+='<div  style="margin-top: 0px;height:30px;width:100%;">';
		 str+='<div  style="text-align:center;color:#ffffff;height:20px; float: left;    font-size: 18px;    margin-left: 23px;"> 站点名称： '+row.deptName+'</div>';
		 
		 str+='</div>';
		 //地址
		 str+='<div  style="margin-top: 0px;height:30px;width:100%;">';
		 str+='<div  style="text-align:center;color:#ffffff;height:20px; float: left;    font-size: 18px;    margin-left: 23px;"> 地址： <span style="font-size: 15px;">'+row.remark+'<span/></div>';
		 
	 }
	 
	 
	 str+='</div>';
	 str+='<p style="border-bottom:solid 1px rgba(53, 131, 158, 0.8);margin: 3px 16px 2px"></p>';
	 if(row.storehouseId != 3){  //库房类型为试验
		 str+='<a onclick="javascript:window.document.location.href=\'account/'+row.dprtcode+'?name='+row.deptName+'\'" style="cursor:pointer;float: right;margin-right: 20px;font-size: 14px;color:#23A6DD;text-decoration:none;" ><i class="fa fa-eye" ></i> 查看详情</a>';
	 }else{
		 str+='<a onclick="javascript:window.document.location.href=\'accountjc/'+row.dprtcode+'?name='+row.deptName+'\'" style="cursor:pointer;float: right;margin-right: 20px;font-size: 14px;color:#23A6DD;text-decoration:none;" ><i class="fa fa-eye" ></i> 查看详情</a>';
	 }
	 
	 
	 str+='</div></div>';
	 
	 return str;
	 
}

/**
 * 点击地图缩放
 */
function onclickmap(){
	javascript:top.location.href='/system/maxmain?dprtcode='+$("#dprtcode").val()+"&deptName="+$("#deptName").val();
}



// 添加带有定位的导航控件
var navigationControl = new BMap.NavigationControl({
  // 靠左上角位置
  anchor: BMAP_ANCHOR_TOP_LEFT,
  // LARGE类型
  type: BMAP_NAVIGATION_CONTROL_LARGE,
});
map.addControl(navigationControl);

